<template>
  <div class="sub-sort">
    <div class="sort">
      <a
        href="javascript:;"
        :class="{ active: !sortField }"
        @click="sortClick(null)"
        >默认排序</a
      >
      <a
        href="javascript:;"
        :class="{ active: sortField === 'publishTime' }"
        @click="sortClick('publishTime')"
        >最新商品</a
      >
      <a
        href="javascript:;"
        :class="{ active: sortField === 'orderNum' }"
        @click="sortClick('orderNum')"
        >最高人气</a
      >
      <a
        href="javascript:;"
        :class="{ active: sortField === 'evaluateNum' }"
        @click="sortClick('evaluateNum')"
        >评论最多</a
      >
      <a href="javascript:;" @click="sortClick('price')">
        价格排序
        <i class="arrow up" :class="{ active: sortMethod === 'asc' }" />
        <i class="arrow down" :class="{ active: sortMethod === 'desc' }" />
      </a>
    </div>
    <div class="check">
      <XtxCheckbox v-model="inventory">仅显示有货商品</XtxCheckbox>
      <XtxCheckbox v-model="onlyDiscount">仅显示特惠商品</XtxCheckbox>
    </div>
  </div>
</template>
<script>
import { reactive, toRefs, watch } from 'vue'
import { onBeforeRouteLeave } from 'vue-router'
export default {
  name: 'SubSort',
  /*
sortField	string	非必须		排序字段，取值范围：[publishTime,orderNum,price,evaluateNum]
sortMethod	string	非必须		排序规则，asc为正序，desc为倒序，默认为desc
inventory	boolean	非必须		是否有库存
onlyDiscount	boolean	非必须		只显示特惠
*/
  setup (props, { emit }) {
    const state = reactive({
      onlyDiscount: false,
      inventory: false,
      sortField: '',
      sortMethod: ''
    })
    // 排序点击
    function sortClick (str) {
      state.sortField = str
      //   如果是价格点击，需要处理规则字段（如果从其它选项切换到price,sortMethod=desc，如果本身就处于price,就是desc与asc的切换）
      if (str === 'price') {
        if (state.sortMethod === '') {
          state.sortMethod = 'desc'
        } else {
          state.sortMethod = state.sortMethod === 'desc' ? 'asc' : 'desc'
        }
      } else {
        // 点击其它选项时还原规则字段
        state.sortMethod = ''
      }
    }
    // 属性修改后要通知父级
    const stop = watch(
      () => state,
      newVal => {
        // console.log('修改了sort排序 ', newVal)
        emit('change', newVal)
      },
      {
        deep: true
      }
    )
    // 离开前清理掉
    onBeforeRouteLeave(() => {
      stop()
    })

    return { ...toRefs(state), sortClick }
  }
}
</script>
<style scoped lang="less">
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
